using System;
using System.Collections.Generic;
using System.Data;
using System.Reflection;
using System.Runtime.Remoting.Messaging;
using UnityEditor;
using UnityEditorInternal;
using UnityEngine;
using XFABManager;

namespace XFGameFramework.MissionSystem
{
    [CustomEditor(typeof(MissionConfigData))]
    public class MissionConfigDataInspector : Editor
    {

        private static MissionConfigDataInspector _instance;

        public static MissionConfigDataInspector Instance
        {
            get
            {
                if (_instance == null)
                    _instance = CreateInstance<MissionConfigDataInspector>();
                return _instance;
            }
        }

        private MissionConfigData currentMissionConfigData;

        private SerializedObject missionConfigSerializedObject = null;

        private List<SerializedProperty> serializedProperties;
          
        private Vector2 currentPosition;
         
        private SerializedProperty m_Script = null;
          

        public override void OnInspectorGUI()
        {

            EditorGUI.BeginDisabledGroup(true); 
            base.OnInspectorGUI(); 
            EditorGUI.EndDisabledGroup();

            //if (GUILayout.Button("编辑任务"))
            //{
            //    ConfigsWindow window = EditorWindow.GetWindow<ConfigsWindow>();
            //    window.Show();

            //    MissionConfigData currentSkill = target as MissionConfigData;
            //    if (currentSkill.parent != null)
            //    {
            //        string asset_path = AssetDatabase.GetAssetPath(currentSkill.parent);
            //        ConfigsWindow.SelectGUID = AssetDatabase.AssetPathToGUID(asset_path);
            //    }

            //    window.SetSelection(currentSkill.id);
            //}

        }


        internal void OnInspectorGUI(MissionConfigData data, ConfigsWindow window,MissionsConfig missionsConfig)
        {
            if (data == null) 
            {
                GUILayout.FlexibleSpace();
                GUILayout.BeginHorizontal();
                GUILayout.FlexibleSpace();
                GUILayout.Label("空");
                GUILayout.FlexibleSpace();
                GUILayout.EndHorizontal();
                GUILayout.FlexibleSpace();

                return;
            }
             
            if (currentMissionConfigData != data)
            { 
                currentMissionConfigData = data;
                missionConfigSerializedObject = null; 
            }

            currentPosition = GUILayout.BeginScrollView(currentPosition);

            if (missionConfigSerializedObject == null)
            {
                missionConfigSerializedObject = new SerializedObject(currentMissionConfigData);
                m_Script = missionConfigSerializedObject.FindProperty("m_Script");
                serializedProperties = missionConfigSerializedObject.GetAllSerializedProperty();
            }

            missionConfigSerializedObject.Update();

            EditorGUI.BeginDisabledGroup(true);

            EditorGUILayout.PropertyField(m_Script);

            EditorGUI.EndDisabledGroup();

            foreach (var property in serializedProperties)
            {

                if (property.name == "parent" && property.objectReferenceValue == null)
                {
                    property.objectReferenceValue = missionsConfig;
                }

                if (GetPropertyCustomAttribute<HideInInspector>(property) != null) continue;
                CustomPropertyGUI customPropertyGUI = GetPropertyCustomAttribute<CustomPropertyGUI>(property);
                EditorGUI.BeginChangeCheck();

                if (customPropertyGUI != null)
                {
                    customPropertyGUI.OnPropertyGUI(property);
                }
                else
                {
                    EditorGUILayout.PropertyField(property);
                }

                if (EditorGUI.EndChangeCheck())
                {
                    missionConfigSerializedObject.ApplyModifiedProperties();

                    if (property.name == "id")
                        window.SetSelection(property.intValue);

                    //EditorUtility.SetDirty(currentMissionConfigData);

                    if (currentMissionConfigData.parent != null)
                        currentMissionConfigData.parent.ClearCache();

                    if (customPropertyGUI != null && customPropertyGUI.reloadTreeViewOnValueChange)
                        window.missionListTree.Reload(); 
                    
                }

            }
             
            GUILayout.EndScrollView();
        }
           
        internal static Texture2D GetImage(string projectName, string assetName)
        {

            if (string.IsNullOrEmpty(projectName) || string.IsNullOrEmpty(assetName))
                return null;

            XFABProject project = XFABProjectManager.Instance.GetProject(projectName);

            if (project != null)
            {
                List<XFABAssetBundle> bundles = project.GetAllAssetBundles();

                foreach (var bundle in bundles)
                {
                    if (bundle == null)
                    {
                        continue;
                    }
                    string asset_path = bundle.GetAssetPathByFileName(assetName, typeof(Texture2D));

                    if (string.IsNullOrEmpty(asset_path))
                        asset_path = bundle.GetAssetPathByFileName(assetName, typeof(Sprite));

                    if (string.IsNullOrEmpty(asset_path))
                        continue;
                    Texture2D texture = AssetDatabase.LoadAssetAtPath<Texture2D>(asset_path);
                    if (texture != null)
                        return texture;
                }
            }

            return null;
        }
         
        private Dictionary<SerializedProperty, Dictionary<Type, Attribute>> attributes = new Dictionary<SerializedProperty, Dictionary<Type, Attribute>>();

        private T GetPropertyCustomAttribute<T>(SerializedProperty property) where T : Attribute
        {
            if (!attributes.ContainsKey(property))
                attributes.Add(property, new Dictionary<Type, Attribute>());

            if (attributes[property].ContainsKey(typeof(T)))
                return attributes[property][typeof(T)] as T;

            Type type = property.serializedObject.targetObject.GetType();
            FieldInfo fieldInfo = type.GetField(property.name, BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance);
            T customPropertyGUI = fieldInfo.GetCustomAttribute<T>();

            attributes[property].Add(typeof(T), customPropertyGUI);
            return customPropertyGUI;
        }

    }

}

